home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / ui / layout / example / diagonallayout.java < prev    next >
Encoding:
Java Source  |  1996-02-26  |  7.5 KB  |  267 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.util.Vector;
  19.  
  20. public class DiagonalLayout implements LayoutManager {
  21.  
  22.     private int vgap;
  23.     private int minWidth = 0, minHeight = 0;
  24.     private int preferredWidth = 0, preferredHeight = 0;
  25.     private boolean sizeUnknown = true;
  26.     private boolean DEBUG = false;
  27.  
  28.     public DiagonalLayout() {
  29.     this(5);
  30.     }
  31.  
  32.     public DiagonalLayout(int v) {
  33.     vgap = v;
  34.     }
  35.  
  36.     /* Required by LayoutManager. */
  37.     public void addLayoutComponent(String name, Component comp) {
  38.     }
  39.  
  40.     /* Required by LayoutManager. */
  41.     public void removeLayoutComponent(Component comp) {
  42.     }
  43.  
  44.     private void setSizes(Container parent) {
  45.     int nComps = parent.countComponents();
  46.     Dimension d = null;
  47.  
  48.     if (DEBUG) {
  49.         System.out.println("");
  50.         System.out.println("setSizes()");
  51.     }
  52.  
  53.     //Reset preferred/minimum width and height.
  54.     preferredWidth = 0;
  55.     preferredHeight = 0;
  56.     minWidth = 0;
  57.     minHeight = 0;
  58.  
  59.     for (int i = 0; i < nComps; i++) {
  60.         Component c = parent.getComponent(i);
  61.         if (c.isVisible()) {
  62.         d = c.preferredSize();
  63.  
  64.         if (i > 0) {
  65.             preferredWidth += d.width/2; 
  66.             preferredHeight += vgap;
  67.         } else {
  68.             preferredWidth = d.width;
  69.         }
  70.         preferredHeight += d.height;
  71.  
  72.         minWidth = Math.max(c.minimumSize().width, minWidth);
  73.         minHeight = preferredHeight;
  74.  
  75.         if (DEBUG) {
  76.             System.out.println("Component["+i+"] preferred size: " + 
  77.             c.preferredSize());
  78.             System.out.println("Component["+i+"] minimum size: " + 
  79.             c.minimumSize());
  80.                 System.out.print("preferred width: " + preferredWidth);
  81.                 System.out.print("; preferred height: " + preferredHeight);
  82.                 System.out.print("; minWidth: " + minWidth);
  83.                 System.out.println("; minHeight: " + minHeight);
  84.         }
  85.         }
  86.     }
  87.     }
  88.  
  89.  
  90.     /* Required by LayoutManager. */
  91.     public Dimension preferredLayoutSize(Container parent) {
  92.     Dimension dim = new Dimension(0, 0);
  93.     int nComps = parent.countComponents();
  94.  
  95.     if (DEBUG) {
  96.         System.out.println("");
  97.         System.out.println("Start of preferredLayoutSize()");
  98.     }
  99.  
  100.     setSizes(parent);
  101.  
  102.     //Always add the container's insets!
  103.     Insets insets = parent.insets();
  104.     dim.width = preferredWidth + insets.left + insets.right;
  105.     dim.height = preferredHeight + insets.top + insets.bottom;
  106.  
  107.     sizeUnknown = false;
  108.  
  109.     if (DEBUG) {
  110.         System.out.println("preferred layout size: " + dim);
  111.         System.out.println("End of preferredLayoutSize()");
  112.         System.out.println("");
  113.     }
  114.     return dim;
  115.     }
  116.  
  117.     /* Required by LayoutManager. */
  118.     public Dimension minimumLayoutSize(Container parent) {
  119.     Dimension dim = new Dimension(0, 0);
  120.     int nComps = parent.countComponents();
  121.  
  122.     if (DEBUG) {
  123.         System.out.println("");
  124.         System.out.println("Start of minimumLayoutSize()");
  125.     }
  126.  
  127.     //Always add the container's insets!
  128.     Insets insets = parent.insets();
  129.     dim.width = minWidth + insets.left + insets.right;
  130.     dim.height = minHeight + insets.top + insets.bottom;
  131.  
  132.     sizeUnknown = false;
  133.  
  134.     if (DEBUG) {
  135.         System.out.println("minimum layout size: " + dim);
  136.         System.out.println("End of minimumLayoutSize()");
  137.         System.out.println("");
  138.     }
  139.     return dim;
  140.     }
  141.  
  142.     /* Required by LayoutManager. */
  143.     /* This is called when the panel is first displayed, 
  144.      * and every time its size changes. 
  145.      * Note: You CAN'T assume preferredLayoutSize() or minimumLayoutSize()
  146.      * will be called -- in the case of applets, at leas, they probably
  147.      * won't be. */
  148.     public void layoutContainer(Container parent) {
  149.     Insets insets = parent.insets();
  150.     int maxWidth = parent.size().width
  151.                - (insets.left + insets.right);
  152.     int maxHeight = parent.size().height
  153.                 - (insets.top + insets.bottom);
  154.     int nComps = parent.countComponents();
  155.     int previousWidth = 0, previousHeight = 0;
  156.     int x = 0, y = insets.top;
  157.     int rowh = 0, start = 0;
  158.     int xFudge = 0, yFudge = 0;
  159.     boolean oneColumn = false;
  160.  
  161.     if (DEBUG) {
  162.         System.out.println("");
  163.         System.out.println("Start of layoutContainer()");
  164.         System.out.println("Container size = " + parent.size());
  165.     }
  166.  
  167.     // Go through the components' sizes, if neither preferredLayoutSize()
  168.     // nor minimumLayoutSize() has been called.
  169.     if (sizeUnknown) {
  170.         if (DEBUG) {
  171.         System.out.println("Calling preferredLayoutSize()");
  172.         }
  173.         setSizes(parent);
  174.     }
  175.         
  176.     if (maxWidth <= minWidth) {
  177.         oneColumn = true;
  178.     }
  179.  
  180.     if (maxWidth != preferredWidth) {
  181.         xFudge = (maxWidth - preferredWidth)/(nComps - 1);
  182.         if (DEBUG) {
  183.         System.out.println("horizontal adjustment = " + xFudge);
  184.         }
  185.     }
  186.  
  187.     if (maxHeight > preferredHeight) {
  188.         yFudge = (maxHeight - preferredHeight)/(nComps - 1);
  189.         if (DEBUG) {
  190.         System.out.println("vertical adjustment = " + yFudge);
  191.         }
  192.     }
  193.  
  194.     for (int i = 0 ; i < nComps ; i++) {
  195.         Component c = parent.getComponent(i);
  196.         if (c.isVisible()) {
  197.         Dimension d = c.preferredSize();
  198.         
  199.          // increase x and y, if appropriate
  200.         if (i > 0) { 
  201.             if (!oneColumn) {
  202.                     //x += previousWidth - d.width/2 + xFudge;
  203.             x += previousWidth/2 + xFudge;
  204.             }
  205.                 y += previousHeight + vgap + yFudge;
  206.         }
  207.         
  208.         if (DEBUG) {
  209.             System.out.println("Placing component["+i+"] at (" +
  210.             x + "," + y + ")");
  211.         }
  212.  
  213.         // If x is too large, ...
  214.         if ((!oneColumn) &&
  215.             (x + d.width) > (parent.size().width - insets.right)) {
  216.             if (DEBUG) {
  217.                 System.out.println("Eek! x too large: x="+x+"; maxX="+
  218.                 (parent.size().width - insets.right));
  219.                 System.out.println("Component["+i+"] preferred width: "
  220.                 +d.width);
  221.             }
  222.             // ... reduce x to a reasonable number.
  223.             x = parent.size().width - insets.bottom - d.width;
  224.             if (DEBUG) {
  225.                 System.out.println("Set x to " + x);
  226.             }
  227.         }
  228.  
  229.         // If y is too large, ...
  230.         if ((y + d.height) > (parent.size().height - insets.bottom)) {
  231.             if (DEBUG) {
  232.                 System.out.println("Eek! y too large: y=" + y +
  233.                 "; height=" + d.height +
  234.                 "; maxY="+ (parent.size().height - insets.bottom));
  235.                 System.out.println("Component["+i+"] preferred height: "
  236.                 +d.height);
  237.             }
  238.             // ... do nothing.
  239.             // Another choice would be to do what we do to x.
  240.         }
  241.  
  242.         // Set the component's size and position.
  243.         c.reshape(x, y, d.width, d.height);
  244.  
  245.         previousWidth = d.width;
  246.         previousHeight = d.height;
  247.         }
  248.     }
  249.  
  250.     if (DEBUG) {
  251.         System.out.println("End of layoutContainer()");
  252.         System.out.println("");
  253.     }
  254.     }
  255.     
  256.     public String toString() {
  257.     String str = "";
  258.     return getClass().getName() + "[vgap=" + vgap + str + "]";
  259.     }
  260. }
  261.  
  262. // TO DO: 
  263. // 1. Come up with a framework for making the Applet just have
  264. //    a button that pops up a window.
  265. // 2. Do event handling for Quit.
  266.  
  267.